package com.heima.article.listener;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class EnableListener {

    @Autowired
    private IApArticleService articleService;

    @KafkaListener(topics = {"${topic.enableTopic}"})
    public void handleMessage(ConsumerRecord<String, String> record) {
        String key = record.key();
        String value = record.value();
        System.out.println("接收到消息: key = " + key + ",value = " + value);
        // 转成文章id
        long articleId = Long.parseLong(key);
        // 更新文章表的已下架状态
        LambdaUpdateWrapper<ApArticle> query = new LambdaUpdateWrapper<>();
        query.eq(ApArticle::getId, articleId);
        query.set(ApArticle::getIsDown, value.equals("false") ? 1 : 0);
        articleService.update(query);
    }
}
